package huanyuxingcheng.nonameinstallhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

public class MainInstallActivity extends BaseActivity implements View.OnClickListener {

    private Spinner spinner;
    /**
     * 一键安装
     */
    private Button installButton;

    private ArrayList<File> files = new ArrayList<>();

    private File file;

    private BaseAdapter baseAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return files.size();
        }

        @Override
        public Object getItem(int i) {
            return files.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null){
                view = View.inflate(MainInstallActivity.this,R.layout.spinner_text_item,null);
            }
            TextView textView = view.findViewById(R.id.item_text);
            File file = files.get(i);
            textView.setText(file.getName());
            return view;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_install);
        initView();
        initSpinner();
    }

    private void initSpinner(){
        File qqFile = NonameUtils.getQQDownloadDirectory();
        files.addAll(NonameUtils.fileFilter(qqFile,"完整包",".zip"));
        files.addAll(NonameUtils.fileFilter(qqFile,"离线包",".zip"));
        if(files.size() == 0){
            showShortToast("未检测到完整包或离线包！");
        }
        spinner.setAdapter(baseAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i >= 0 && files.size() > 0 && files.size() > i) {
                    file = files.get(i);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                file = null;
            }
        });
    }

    private void initView() {
        spinner = findViewById(R.id.spinner);
        installButton = findViewById(R.id.install_button);
        installButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.install_button:
                if(file != null){
                    Intent intent = new Intent(this,ZipActivity.class);
                    intent.putExtra("fromPath",file.getAbsolutePath());
                    intent.putExtra("toPath",NonameUtils.getNonameDirectory().getAbsolutePath());
                    startActivity(intent);
                }else {
                    showShortToast("未选择文件！");
                }
                break;
        }
    }
}
